package com.sense.kukoo.common.wiget;

import android.app.Activity;
import android.content.Context;
import android.content.res.ColorStateList;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;

import com.sense.kukoo.common.R;

public class TittleView extends Toolbar {

    private ImageView iv_back;
    private TextView tv_title;
    private View divider;

    private CharSequence mTitle;
    private ColorStateList mTitleColor;

    public TittleView(@NonNull Context context) {
        super(context);
        initView(context);
    }

    public TittleView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public TittleView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(@NonNull Context context) {
        setContentInsetsAbsolute(0, 0);
        LayoutInflater.from(getContext()).inflate(R.layout.common_title, this, true);
        tv_title = findViewById(R.id.tv_title);
        iv_back = findViewById(R.id.iv_back);
        divider = findViewById(R.id.divider);
        //
        if (mTitle != null) {
            tv_title.setText(mTitle);
        }
        if (mTitleColor != null) {
            tv_title.setTextColor(mTitleColor);
        } else {
            tv_title.setTextColor(getResources().getColor(R.color.android_black));
        }
    }

    @Override
    public void setTitle(CharSequence title) {
        mTitle = title;
        super.setTitle("");
        if (tv_title != null) {
            tv_title.setText(title);
        }
    }

    @Override
    public void setTitleTextColor(@NonNull ColorStateList color) {
        mTitleColor = color;
        super.setTitleTextColor(color);
        if (tv_title != null) {
            tv_title.setTextColor(color);
        }
    }

    public void init(Activity activity) {
        iv_back.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                activity.finish();
            }
        });
    }

    public void init(Activity activity, String title, boolean isShowDivider) {
        init(activity);
        setTitle(title);
        divider.setVisibility(isShowDivider ? View.VISIBLE : View.INVISIBLE);
    }

}
